package com.watersys.checkwork.activity;


import java.util.HashMap;
import java.util.Map;

import com.baidu.location.LocationClient;
import com.watersys.R;
import com.watersys.httpserver.HttpClientServer;
import com.watersys.util.BaiDuGpsUtil;
import com.watersys.util.SharedPreferenceUtil;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class CheckWorkMainActivity extends Activity{
	
	  public static final String username="USERNAME";//存取用户名称标志
	  
	  public static final String gotowork="";//上班请求标志
	  
	  public static final String gooffwork="";//下班请求标志
	  
	  public static final String workstatus="WORKSTATUS";//上班状态
	
     public EditText edit1;//用户姓名
     
     public EditText edit2;//用户密码
     
     public Button button1;//上班
     
     public Button button2;//下班
     
    public  SharedPreferenceUtil sp=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checkworkmain);
	}
     
	/**
	 * 初始化
	 */
	public void inti(){
		edit1=(EditText)findViewById(R.id.checkwork_username);
		edit2=(EditText)findViewById(R.id.checkwork_pass);
		button1=(Button)findViewById(R.id.gotowork);
		button2=(Button)findViewById(R.id.gooffwork);
		sp=new SharedPreferenceUtil(this);
		String name=sp.getValue(username);
		
		if(!TextUtils.isEmpty(name)){
			edit1.setText(name);
		}
	}
	
	/**
	 * 添加上下班事件
	 */
	public void addListent(){
		
		//上班事件
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 SharedPreferenceUtil sp=new SharedPreferenceUtil(CheckWorkMainActivity.this);
				boolean b=sp.getBoolean(workstatus);
				if(b){
					 Toast.makeText(CheckWorkMainActivity.this, "今天已打过上班卡", Toast.LENGTH_LONG).show();
					 finish();
					 return;
				}
				LocationClient client=new LocationClient(CheckWorkMainActivity.this);
				client.start();
				BaiDuGpsUtil gps=new BaiDuGpsUtil(client);
				Double[] d=gps.getLoDoubles();
				
				//得到网络服务
			   HttpClientServer server=new HttpClientServer(CheckWorkMainActivity.this, gotowork);
			   
			   //上班传给后台参数
			   Map<String, String> map=new HashMap<String, String>();
			   map.put("username", edit1.getEditableText().toString());
			   map.put("userpass", edit2.getEditableText().toString());
			   map.put("Latitude", d[0]+"");
			   map.put("Longitude", d[1]+"");
			   String respon=server.doPost(map);
			   if(!respon.equals("error")){
				 Toast.makeText(CheckWorkMainActivity.this, "上班打卡成功", Toast.LENGTH_LONG).show();
				
				 //把用户名字存入XML
				 sp.insertValue(username, edit1.getEditableText().toString());
				 //更新上班状态
				 sp.insertValue(workstatus, true);
				 finish();
			   }else{
				   Toast.makeText(CheckWorkMainActivity.this, "网络连接错误，请检查", Toast.LENGTH_LONG).show();
			   }
				
			}
		});
		
		//下班事件
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
